﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace webapp4U.UI
{
    public partial class WebMaster_Default : System.Web.UI.Page
    {
        int ProductID = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["Action"] == "Edit")
            {
                ProductID = Globals.GetIntFromQueryString("ProductID");
                btSave.Text = ResourceManager.GetString("Update");
                if (!IsPostBack)
                    Load_ProductByID();
            }
        }


        private void Load_ProductByID()
        {
            webapp4U.BOL.Products obj = webapp4U.BOL.Products.GetProductsByID(ProductID);
            HPhotoFile.Value = obj.ImageUrl;
            txtProductCode.Text = obj.ProductCode;
            txtProductName.Text = obj.Title;
            txtPrice.Text = Formatter.FormatMoney(obj.UnitPrice).ToString();
            dlCategory.SelectedValue = obj.CateID.ToString();
            dlCompany.SelectedValue = obj.CompanyID.ToString();
            dlChatlieu.SelectedValue = obj.ChatlieuID.ToString();
            txtNam.Text = obj.Nam;
            txtTrongluong.Text = obj.Trongluong;
            txtDesc.Text = obj.Description.Replace("<br>","\n");
        }


        protected void btSave_Click(object sender, EventArgs e)
        {
            webapp4U.BOL.Products obj = new webapp4U.BOL.Products();
            obj.AddedBy = Globals.CurrentUserName;
            obj.AddedDate = DateTime.Now;
            obj.CateID = Globals.ConvertToInt32(dlCategory.SelectedItem.Value);
            obj.CompanyID = Globals.ConvertToInt32(dlCompany.SelectedItem.Value);
            obj.ProductID = this.ProductID;
            obj.ProductCode = txtProductCode.Text;
            obj.Title = txtProductName.Text;
            obj.ChatlieuID = Globals.ConvertToInt32(dlChatlieu.SelectedItem.Value);
            obj.Nam = txtNam.Text;
            obj.Trongluong = txtTrongluong.Text;
            obj.UnitPrice = 0;
            if(txtPrice.Text.Trim()!="")
                obj.UnitPrice = Convert.ToDecimal(txtPrice.Text.Replace(".",""));

            obj.ImageUrl = Photo_Upload();
            obj.Description = txtDesc.Text.Replace(Environment.NewLine,"<br>");


            if (Globals.GetStringFromQueryString("Action") == "Edit")
            {
                webapp4U.BOL.Products.Update(obj);
            }
            else
            {
                webapp4U.BOL.Products.Insert(obj);
                
                // xoa du lieu cu
                //HPhotoFile.Value = "";
                //txtProductCode.Text = "";
                //txtProductName.Text = "";
                //txtPrice.Text = "";
                //txtDesc.Text = "";
            }
            PanelMsg.Visible = true;
            lblMsg.Text = ResourceManager.GetString("ModifiedSuccessfully");

            Response.Redirect(Globals.ApplicationPath + "WebMaster/Products/");
        }


        protected string Photo_Upload()
        {
            string strFileName = "";
            // kiem tra xem user co chon hinh dai dien ko
            if (PhotoFile.HasFile)
            {
                // kiem tra phan mo rong cua file co hop le hay khong
                string strExtension = System.IO.Path.GetExtension(PhotoFile.FileName).ToUpper();
                if (strExtension == ".JPG" || strExtension == ".GIF" || strExtension == ".JPEG" || strExtension == ".PNG" || strExtension == ".BMP")
                {
                    // lay ten file de khong bi trung file + phan mo rong cua file can upload
                    strFileName = Common.GetFileName() + System.IO.Path.GetExtension(PhotoFile.FileName);

                    //duong dan tao save file Large
                    string strFilePathLarge = Request.PhysicalApplicationPath + "/UserUpload/Products/" + strFileName;

                    //thuc hien save file Large len server
                    EnumUpload uploadLarge = FileProcess.UpLoadImage(PhotoFile, strFilePathLarge, 400);
                }
                else
                {
                    lblMsg.Text = "File bạn chọn upload không hợp lệ.";
                    return strFileName = "";
                }
            }
            else
            {
                return HPhotoFile.Value.Trim();
            }

            return strFileName;
        }
    }
}